###
#
# Data Localization Project
# 
# Trade Agreements Map
#
# Authors: Tyler Girard and Alex Wilhelm
#
#
#
###

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Load Packages ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

library(here)
library(tidyverse)
library(rio)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Set Seed ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

set.seed(515)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Load Data ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

df <- import(here("1 Data", "TAPED_Dataset_Burri_Vasquez_Kugler_November_2022.xlsx"))


head(df)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Prep Data ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

pdat <- df %>%
  select(taped_number, long_title, short_title, type, type_memb, parties, year_in_force, 
         digchap=data_flo_lim_proh_loc_2_3_3, outchap=data_flow_proh_loc_2_2_3) %>%
  mutate(year_in_force = na_if(year_in_force, "N/A")) %>%
  mutate(digchap = na_if(digchap, "N/A")) %>%
  mutate(year_in_force = na_if(year_in_force, "0")) %>%
  drop_na(year_in_force) %>%
  mutate(digchap = as.numeric(digchap)) %>%
  mutate(ex = case_when(digchap == 0 & outchap == 0 ~ 0,
                        TRUE ~ 1)) %>%
  filter(ex == 1)

head(pdat)
summary(pdat)

test <- pdat$parties

ls <- list()
for(i in 1:length(test)){
  ls[[i]] <- str_extract_all(test[i], "\\b[a-zA-Z]{3}\\b")[[1]]
}

newls <- sort(unique(unlist(ls)))


# create data for world coordinates using 
# map_data() function
world_coordinates <- map_data("world")

world_coordinates <- world_coordinates %>%
  mutate(tradeagreement = case_when(region == "Argentina" |
                                      region == "Australia" |
                                      region == "Brazil" |
                                      region == "Brunei" |
                                      region == "Canada" |
                                      region == "Chile" |
                                      region == "China" |
                                      region == "Colombia" |
                                      region == "UK" |
                                      region == "Indonesia" |
                                      region == "Japan" |
                                      region == "Cambodia" |
                                      region == "South Korea" |
                                      region == "Laos" |
                                      region == "Sri Lanka" |
                                      region == "Mexico" |
                                      region == "Mongolia" |
                                      region == "Malaysia" |
                                      region == "New Zealand" |
                                      region == "Peru" |
                                      region == "Philippines" |
                                      region == "Singapore" |
                                      region == "Thailand" |
                                      region == "Uruguay" |
                                      region == "USA" |
                                      region == "Vietnam" |
                                      # rest of ASEAN
                                      region == "Myanmar" |
                                      region == "Malaysia" ~ "Yes",
                                    TRUE ~ "No")) %>%
  filter(region != "Antarctica")

head(world_coordinates)

# create world map using ggplot() function
ggplot() +
  
  # geom_map() function takes world coordinates as input 
  # to plot world map color parameter determines the 
  # color of borders in map fill parameter determines 
  # the color of fill in map size determines the thickness
  # of border in map
  geom_map(
    data = world_coordinates, map = world_coordinates,
    aes(long, lat, map_id = region, fill = tradeagreement),
    color = "black", size = 0.2
  )

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Final Map for Paper ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

datalocalmap <- ggplot(world_coordinates, aes(long, lat, group = group)) +
  coord_fixed(1.3)+
  geom_polygon(aes(fill = tradeagreement), color = "black") +
  scale_fill_manual(values = c("lightgrey", "royalblue")) +
  theme_minimal() +
  theme (panel.grid.major = element_blank(),
         panel.grid.minor = element_blank()) +
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        rect = element_blank()) +
  theme (legend.position = "bottom",
         legend.title = element_text(color = "black", size=10, family = "sans"),
         legend.text = element_text (color = "black", size =8, family = "sans"),
         legend.key.size = unit(0.3, 'cm'),
         legend.key.height = unit(0.3, 'cm'),
         legend.key.width = unit(0.3, 'cm'), 
  )+
  labs(
    x = "",
    y = "",
    fill = "Party to Trade Agreement with Restriction on Use of Data Localization Policies"
  )


datalocalmap

ggsave(datalocalmap, filename = here("2 Figures", "Data Localization Trade Agreements Map.png"), scale = 1)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### End of Script ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

